package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketFootprintMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: chenjian
 * @Date: 2024/5/19 19:21
 */
@Service
public class WxFootPrintServiceImpl implements WxFootPrintService {
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public List<MarketGoods> list(int page, int limit, int userId) {
        List<MarketGoods> list=new ArrayList<>();
        //查找用户浏览商品的goodsId
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria1 = marketFootprintExample.createCriteria();
        //足迹是否删除
        criteria1.andDeletedEqualTo(false);
        criteria1.andUserIdEqualTo(userId);
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);

        //根据商品id,查找商品列表

        for (MarketFootprint marketFootprint : marketFootprints) {
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            System.out.println(marketFootprint.getGoodsId());
            MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
            criteria.andIdEqualTo(marketFootprint.getGoodsId());
            criteria.andDeletedEqualTo(false);
            MarketGoods marketgoods = marketGoodsMapper.selectOneByExampleSelective(marketGoodsExample);
            list.add(marketgoods);
        }
        return list;
    }

    @Override
    public Boolean delete(String GoodsId) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria marketGoodsExampleCriteria = marketGoodsExample.createCriteria();
        marketGoodsExampleCriteria.andGoodsSnEqualTo(GoodsId);
        MarketGoods marketGoods = marketGoodsMapper.selectOneByExampleSelective(marketGoodsExample);
        marketGoods.setDeleted(true);
        int i = marketGoodsMapper.updateByExample(marketGoods, marketGoodsExample);
        if(i >0)
        return true;
        else
            return false;
    }

    @Override
    public MarketFootprint addFootprint(int userId, Integer goodsId, LocalDateTime addTime, LocalDateTime updateTime) {
       try {
           MarketFootprint marketFootprint = new MarketFootprint();
           marketFootprint.setUserId(userId);
           marketFootprint.setGoodsId(goodsId);
           marketFootprint.setAddTime(addTime);
           marketFootprint.setUpdateTime(updateTime);
           marketFootprint.setDeleted(false);
           marketFootprintMapper.insertSelective(marketFootprint);
           return marketFootprint;
       }catch (Exception e){
           return null;
       }


    }
}
